import { _decorator, CircleCollider2D, Component, PhysicsGroup, RigidBody2D, sp, Sprite, SpriteFrame, UITransform, Vec2 } from 'cc';
import { $planet_PLANET_CONF_poof$, $planet_PLANET_ID_LIST_poof$, $planet_PLANET_SCALE_poof$ } from '../../config.global';
import type { PlanetContainer } from '../ui/planet-container';
const { ccclass, property } = _decorator;

@ccclass('Planet')
export class Planet extends Component {
	@property(RigidBody2D)
	private body: RigidBody2D = null;
	@property(CircleCollider2D)
	private collider: CircleCollider2D = null;

	private $planet_ui_poof$: UITransform;
	private $planet_container_poof$: PlanetContainer;

	private $planet___data___poof$: Partial<abd.$planet_Fusion_poof$<typeof $planet_PLANET_CONF_poof$[number], {red: boolean}>> = {};
	private $planet___sleep___poof$: boolean = true;

	public get $planet_data_poof$(){
		return this.$planet___data___poof$??{};
	}
	public get $planet_is_sleep_poof$(){
		return this.$planet___sleep___poof$;
	}
	public get $planet_size_poof$(){
		return this.$planet_ui_poof$.contentSize;
	}
	public get $planet_width_poof$(){
		return this.$planet_ui_poof$.width;
	}
	public get $planet_height_poof$(){
		return this.$planet_ui_poof$.height;
	}

	//private $planet_pxi_poof$: {width: number, height: number, radius: number};

	public $planet_setPlanet_poof$(options: {id: typeof $planet_PLANET_ID_LIST_poof$[number], spriteFrame?: SpriteFrame, red?: boolean}){
		if(!this.$planet_ui_poof$) this.$planet_init_poof$();
		this.$planet_clear_poof$();
		this.$planet___data___poof$.id = options.id;
		this.$planet___data___poof$.red = options.red??false;
		this.node.name = 'planet:'+options.id;

		let sprite = this.node.getComponent(Sprite)
		if(options.spriteFrame && !Planet.$planet_sameSpriteFrame_poof$(options.spriteFrame, sprite.spriteFrame)){
			sprite.spriteFrame = options.spriteFrame;
			sprite.enabled = true;
		}

		let size = sprite.spriteFrame.originalSize;
		let [width, height] = [size.width, size.height].map(v=>v * $planet_PLANET_SCALE_poof$);
		let radius = this.$planet_tweenShape_poof$(width, height);

		let conf = $planet_PLANET_CONF_poof$.find(el=>el.id===options.id);
		if(conf){
			this.body.gravityScale = conf.gravity;
			this.body.linearDamping = 0;

			this.collider.density = conf.density;
			this.collider.apply();
			if(conf.area===undefined) conf.area = Math.PI * Math.pow(radius, 2);
			Object.assign(this.$planet___data___poof$, conf);
		}

		return this;
	}

	public $planet_wakeUp_poof$(container: PlanetContainer){
		this.$planet_container_poof$ = container;
		if(this.$planet___sleep___poof$===false) return void 0;
		this.$planet___sleep___poof$ = false;
		this.body.group = Reflect.get(PhysicsGroup, 'planet');
		this.collider.group = this.body.group;
		this.body.gravityScale = this.$planet_data_poof$.gravity;
		this.body.wakeUp();
		this.unscheduleAllCallbacks();
	}

	public $planet_sleep_poof$(){
		if(this.$planet___sleep___poof$) return void 0;
		this.$planet___sleep___poof$ = true;
		this.body.group = Reflect.get(PhysicsGroup, 'STATIC');
		this.collider.group = this.body.group;
		this.body.gravityScale = 0;
		this.body.linearVelocity = Vec2.ZERO;
		this.body.sleep();
	}

	public $planet_shutdown_poof$(){
		this.body.enabled = false;
		this.collider.enabled = false;
		this.enabled = false;
	}

	unuse(){
		this.$planet_clear_poof$();
	}

	reuse(){
		this.body.awakeOnLoad = true;
		this.body.allowSleep = true;
		this.$planet_sleep_poof$();
		this.node.active = true;
	}

	private static $planet_sameSpriteFrame_poof$(s1: SpriteFrame, s2: SpriteFrame){
		return s1 && s2 && s1.name===s2.name;
	}

	private get $planet_in_container_poof$(){
		return this.node.position.y + this.$planet_ui_poof$.height*(1-this.$planet_ui_poof$.anchorY) <= this.$planet_container_poof$.$planet_top_poof$;
	}

	private $planet_init_poof$(){
		this.$planet_ui_poof$ = this.node.getComponent(UITransform);
		//this.$planet_pxi_poof$ = {} as any;
		//let that = this;
		//Reflect.defineProperty(this.$planet_pxi_poof$, 'width', {
		//	get() {
		//		return that.$planet_ui_poof$.width;
		//	},
		//	set(v) {
		//		that.$planet_ui_poof$.width = v;
		//	},
		//	enumerable: true,
		//	configurable: false,
		//});
		//Reflect.defineProperty(this.$planet_pxi_poof$, 'height', {
		//	get() {
		//		return that.$planet_ui_poof$.height;
		//	},
		//	set(v) {
		//		that.$planet_ui_poof$.height = v;
		//	},
		//	enumerable: true,
		//	configurable: false,
		//});
		//Reflect.defineProperty(this.$planet_pxi_poof$, 'radius', {
		//	get() {
		//		return that.collider.radius;
		//	},
		//	set(v) {
		//		that.collider.radius = v;
		//		that.collider.apply();
		//	},
		//	enumerable: true,
		//	configurable: false,
		//});
	};

	private $planet_clear_poof$(){
		this.getComponentsInChildren(sp.Skeleton).forEach(spine=>spine.clearTracks());
		this.node.children.forEach(node=>node.active = false);
		//if(this.$planet_pxi_poof$) Tween.stopAllByTarget(this.$planet_pxi_poof$);
	}

	private $planet_tweenShape_poof$(width: number, height: number){
		let radius = Math.max(width, height)/2;
		if([this.$planet_ui_poof$.width==width, this.$planet_ui_poof$.height==height, this.collider.radius==radius].every(_=>_)) return radius;

		this.$planet_ui_poof$.setContentSize(width, height);
		this.collider.radius = radius;
		this.collider.apply();

		//tween(this.$planet_pxi_poof$).to(0.1, {width, height, radius}).start();

		return radius;
	}

	//private $planet_onBeginContact_poof$(self: Collider2D, other: Collider2D){
	//	// 忽略条件
	//	if([
	//		!this.body.isAwake(),
	//		// 只处理球球之间的碰撞
	//		self.group===other.group,
	//		// 球球没完全进入容器之前
	//		this.$planet_in_container_poof$,
	//	].includes(false)) return void 0;

	//	this.body.enabledContactListener = false;
	//}
}

